package com.sumainfor.pc.epc.service.impl;

import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.common.utlis.Pager;
import com.sumainfor.pc.epc.dao.RepairEvaMapper;
import com.sumainfor.pc.epc.feign.GmioFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZpcMktFeign;
import com.sumainfor.pc.epc.feign.Zfeign.ZzuulFeign;
import com.sumainfor.pc.epc.service.RepairEvaService;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 */
@Service
public class RepairEvaServiceImpl implements RepairEvaService {

    private ReentrantLock lock = new ReentrantLock(false);
    Logger log= LoggerFactory.getLogger(RepairEvaServiceImpl.class);
    @Autowired
    private RepairEvaMapper repairEvaMapper;
    @Autowired
    private ZzuulFeign zzuulFeign;
    @Autowired
    private ZpcMktFeign zpcMktFeign;
    @Autowired
    private GmioFeign gmioFeign;


    @Override
    public JsonResult getRepairEvaNoList(Map<String, Object> params,Pager pager) {
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> HouIdList=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
//        if (!params.get("Province").toString().equals("")
//                ||  !params.get("City").toString().equals("")
//                ||  !params.get("District").toString().equals("")
//                ||  !params.get("Street").toString().equals("")
//                ||  !params.get("ComId").toString().equals("")
//                ||  !params.get("BuilDing").toString().equals("")
//                ||  !params.get("Room").toString().equals("")
//        ){
            if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"City")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"District")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"BuilDing")
                    ||  FieldCheckUtil.isMapFieldNotNull(params ,"Room")
            ){
          JsonResult HouFeign = zpcMktFeign.getHouIdList(params);
            if (HouFeign.get("errorCode").equals("0")) {
                HouIdList=(List<Map<String,Object>>)HouFeign.get("result");
                params.put("HouIdList",HouIdList);
            }

//            JsonResult comFeign=zpcMktFeign.getComIdList(params);
//            if (comFeign.get("errorCode").equals("0")) {
//                comlist=(List<Map<String,Object>>)comFeign.get("result");
//                params.put("comlist",comlist);
//            }
        }
        pager.setPagerNecessary(params, pager);
        List<Map<String, Object>> epcList = repairEvaMapper.getRepairEvaNoList(params);
        Integer epcCount=repairEvaMapper.getRepairEvaNoListCount(params);
        if(epcList.size()>0) {
            //保存维修工单的房源编号
            List<String> houIdList = new ArrayList<String>();
            //保存管家报修人编号
            List<String> uidList=new ArrayList<String>();
            //保存客户报修人编号
            List<String> tidList=new ArrayList<String>();
            //保存维修师傅的编号
            List<String> sysList=new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null) {
                    //根据 HouId查询房源信息
                    houIdList.add(map.get("HouCon").toString());
                }
                if(map.get("RepType")!=null){
                    //是否通过管家巡房发起  0.否   1是'
                    if (map.get("RepType").toString().equals("2")){
                        if(map.get("RepairsId") !=null){
                            uidList.add(map.get("RepairsId").toString());
                        }
                    }else{
                        if(map.get("RepairsId") !=null){
                            tidList.add(map.get("RepairsId").toString());
                        }
                    }
                }
                if (map.get("SysUserId")!=null){
                    sysList.add(map.get("SysUserId").toString());
                }
            }
            System.out.println("传递到网关的参数uidList:"+uidList);
            System.out.println("传递到网关的参数:"+sysList);
            //根据房源编号到市场部查询房源基本信息
            if(uidList.size()>0 || sysList.size()>0){//把租客的编号传到市场部查询租客即报修人的信息
                params.put("uidList",uidList);
                params.put("sysList",sysList);
                System.out.println("传递到网关的参数:"+params);
                JsonResult zuulResult=zzuulFeign.getRuserList(params);
                Map<String,Object> roMap=(Map<String,Object>)zuulResult.get("result");
                //报修人信息
                List<Map<String,Object>> zuulList=(List<Map<String,Object>>)roMap.get("rList");
                //受理人信息
                List<Map<String,Object>> zuuolList=(List<Map<String,Object>>)roMap.get("oList");
                if (zuulResult.get("errorCode").equals("0")) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null){
                            for (Map<String, Object> map2 : zuulList) {
                                if (map2.get("user_id")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                    for (Map<String, Object> map : epcList) {
                        if (map.get("SysUserId")!=null) {
                            for (Map<String, Object> map2 : zuuolList) {
                                if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "user_id"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            //把房源详表编号传递到市场部
            params.put("idList",houIdList);
            //把租客编号传递到市场部
            System.out.println("");
            params.put("TenantList",tidList);
            System.out.println("传递到市场部的参数:"+params);
            JsonResult result = zpcMktFeign.getCommunityList(params, pager);
            //房源信息
            List<Map<String, Object>> mktList = (List<Map<String, Object>>) result.get("result");
            if (result.get("errorCode").equals("0")) {
                for (Map<String, Object> map : epcList) {
                    if (map.get("HouCon")!=null) {
                        for (Map<String, Object> map2 : mktList) {
                            if (map2.get("HouCon")!=null) {
                                if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
                //租客报修人信息
                List<Map<String, Object>> tmktList = (List<Map<String, Object>>) result.get("tenList");
                System.out.println("resuyktepcList"+epcList);
                System.out.println("resuykt"+tmktList);
                if (tmktList!=null) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null) {
                            for (Map<String, Object> map2 : tmktList) {
                                if (map2.get("TenId")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //维修账单
            for (Map<String,Object> map1:epcList) {
                if (map1.get("OrdBiId")!=null){
                    map1.put("Source",map1.get("OrdBiId"));
                    JsonResult result2 = gmioFeign.getRepairBillMapOne(map1);
                    Map<String,Object> repairMap1 = (Map<String, Object>) result2.get("result");
                    map1.putAll(repairMap1);
                }
            }


            return pageUtils.getJsonResult(epcList, params,epcCount);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }
    /**
     * 查询所有维修工单评价
     * @param params
     * @return
     */
    @Override
    public JsonResult getRepairEvaList(Map<String, Object> params,Pager pager) {
        //维修师傅不能看到对机子的评价
        String SysUserId = ContextUtils.getUserId();
        if (SysUserId !=null&&SysUserId.equals("24")){
            params.put("SysUserId",SysUserId);
        }

        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> HouIdList=new ArrayList<Map<String,Object>>();
        //是否有选择省市区条件
//        if (!params.get("Province").toString().equals("")
//                ||  !params.get("City").toString().equals("")
//                ||  !params.get("District").toString().equals("")
//                ||  !params.get("Street").toString().equals("")
//                ||  !params.get("ComId").toString().equals("")
//                ||  !params.get("BuilDing").toString().equals("")
//                ||  !params.get("Room").toString().equals("")
//        ){

        if (FieldCheckUtil.isMapFieldNotNull(params ,"Province")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"City")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"District")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"Street")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"ComId")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"BuilDing")
                ||  FieldCheckUtil.isMapFieldNotNull(params ,"Room")
        ){
            JsonResult HouFeign = zpcMktFeign.getHouIdList(params);
            if (HouFeign.get("errorCode").equals("0")) {
                HouIdList=(List<Map<String,Object>>)HouFeign.get("result");
                params.put("HouIdList",HouIdList);
            }
//            JsonResult comFeign=zpcMktFeign.getComIdList(params);
//            if (comFeign.get("errorCode").equals("0")) {
//                comlist=(List<Map<String,Object>>)comFeign.get("result");
//                params.put("comlist",comlist);
//            }
        }
        pager.setPagerNecessary(params, pager);
        List<Map<String, Object>> epcList = repairEvaMapper.getRepairEvaList(params);
        Integer epcCount=repairEvaMapper.getRepairEvaListCount(params);
        if(epcList.size()>0) {
            //保存维修工单的房源编号
            List<String> houIdList = new ArrayList<String>();
            //保存管家报修人编号
            List<String> uidList=new ArrayList<String>();
            //保存客户报修人编号
            List<String> tidList=new ArrayList<String>();
            //保存维修师傅的编号
            List<String> sysList=new ArrayList<String>();
            for (Map<String, Object> map : epcList) {
                if (map.get("HouCon") != null) {
                    //根据 HouId查询房源信息
                    houIdList.add(map.get("HouCon").toString());
                }
                if(map.get("RepType")!=null){
                    //是否通过管家巡房发起  0.否   1是'
                    if (map.get("RepType").toString().equals("2")){
                        if(map.get("RepairsId") !=null){
                            uidList.add(map.get("RepairsId").toString());
                        }
                    }else{
                        if(map.get("RepairsId") !=null){
                            tidList.add(map.get("RepairsId").toString());
                        }
                    }
                }
                if (map.get("SysUserId")!=null){
                    sysList.add(map.get("SysUserId").toString());
                }
            }
            System.out.println("传递到网关的参数uidList:"+uidList);
            System.out.println("传递到网关的参数:"+sysList);
            //根据房源编号到市场部查询房源基本信息
            if(uidList.size()>0 || sysList.size()>0){//把租客的编号传到市场部查询租客即报修人的信息
                params.put("uidList",uidList);
                params.put("sysList",sysList);
                System.out.println("传递到网关的参数:"+params);
                JsonResult zuulResult=zzuulFeign.getRuserList(params);
                Map<String,Object> roMap=(Map<String,Object>)zuulResult.get("result");
                //报修人信息
                List<Map<String,Object>> zuulList=(List<Map<String,Object>>)roMap.get("rList");
                //受理人信息
                List<Map<String,Object>> zuuolList=(List<Map<String,Object>>)roMap.get("oList");
                if (zuulResult.get("errorCode").equals("0")) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null){
                            for (Map<String, Object> map2 : zuulList) {
                                if (map2.get("user_id")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "user_id"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                    for (Map<String, Object> map : epcList) {
                        if (map.get("SysUserId")!=null) {
                            for (Map<String, Object> map2 : zuuolList) {
                                if (MapUtils.getString(map, "SysUserId").equals(MapUtils.getString(map2, "user_id"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
            }
            //把房源详表编号传递到市场部
            params.put("idList",houIdList);
            //把租客编号传递到市场部
            System.out.println("");
            params.put("TenantList",tidList);
            System.out.println("传递到市场部的参数:"+params);
            JsonResult result = zpcMktFeign.getCommunityList(params, pager);
            //房源信息
            List<Map<String, Object>> mktList = (List<Map<String, Object>>) result.get("result");
            if (result.get("errorCode").equals("0")) {
                for (Map<String, Object> map : epcList) {
                    if (map.get("HouCon")!=null) {
                        for (Map<String, Object> map2 : mktList) {
                            if (map2.get("HouCon")!=null) {
                                if (MapUtils.getString(map, "HouCon").equals(MapUtils.getString(map2, "HouCon"))) {
                                    map.putAll(map2);
                                }
                            }
                        }
                    }
                }
                //租客报修人信息
                List<Map<String, Object>> tmktList = (List<Map<String, Object>>) result.get("tenList");
                System.out.println("resuyktepcList"+epcList);
                System.out.println("resuykt"+tmktList);
                if (tmktList!=null) {
                    for (Map<String, Object> map : epcList) {
                        if (map.get("RepairsId")!=null) {
                            for (Map<String, Object> map2 : tmktList) {
                                if (map2.get("TenId")!=null) {
                                    if (MapUtils.getString(map, "RepairsId").equals(MapUtils.getString(map2, "TenId"))) {
                                        map.putAll(map2);
                                    }
                                }
                            }
                        }
                    }
                }
            }

            //维修账单
            for (Map<String,Object> map1:epcList) {
                if (map1.get("OrdBiId")!=null){
                    map1.put("Source",map1.get("OrdBiId"));
            JsonResult result2 = gmioFeign.getRepairBillMapOne(map1);
            Map<String,Object> repairMap1 = (Map<String, Object>) result2.get("result");
            if (repairMap1!=null) {
                map1.putAll(repairMap1);
            }
                }
            }

            return pageUtils.getJsonResult(epcList, params,epcCount);
        }
        return pageUtils.getJsonResult(new ArrayList<Map<String,Object>>(), params,0);
    }


    /**
     * 根据维修钢弹编号查询领用单
     * @author zhoucong
     * @data 2019/10/22
     * @Param
     * @return
     */
    public JsonResult getRepIdReceive(Map<String,Object> params){
        JsonResult result = new JsonResult();
        return result.put(repairEvaMapper.getRepIdReceive(params));
    }



}
